!-----------------------------------------------------------------------
!  Copyright (c) 2020 Feasa Enterprises Ltd.
!  Rev 3.0  August 2020
!
!  All Rights Reserved.  Reproduction, adaptation, or translation
!  without prior written permission is prohibited, except as allowed
!  under the copyright laws.
!
!-----------------------------------------------------------------------
!
! Device        : Feasa_20i
! Manufacturer  : Feasa Enterprises Ltd.
! Description   : 20 Channel device to interrogate a LED
! Package       : 20 pin
! Test Platform : 3070
! Safeguard     : cmos_3v3
!
!-----------------------------------------------------------------------
! WARNING:  This is a Led Analyser function referenced by a PDL.
!
!   This test is not intended to be used directly for a Led analyser.
!   Instead, a part description library for the Led analyser should be
!   used where the part description language of that file refers to
!   this file as the part number of one of it's several "sub-components" .
!   Notice also that this test uses signal names rather than ordinal
!   pin numbers.  The referencing part description library should map
!   the device's actual pin numbers to these signal names.
!
!
! Additional Comments:
!
!   This test send the capture time, wait time and capture sequence number.
!   to the LED Analyser.  These values are in msec and are passed in through
!   5 arrays
!-----------------------------------------------------------------------
!
!****************************************************************************
!      Synchronous Serial Model for the Feasa LED Analyser
!
!Note: All commands sent to the Led Analyser use the following format
! To send a command
!       - Set Ser_in to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Set Ser_in to Bit 6
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Continue until all 8 bits have been clocked into the Led Analyser
!
!----------------------------------------------------------------------------


test digital; Data1(*),Data2(*),Data3(*),Data4(*),Data5(*),Data6(*),Data7(*)

vector cycle  20u      ! minimum time 9u
receive delay 19u      ! minimum time 8u

test time 1000m

default device "#%feasa_20i%la_seq_capt_setup_ss"
set terminators to on

assign Ser_out            to pins "Ser_out"
assign Ser_in             to pins "Ser_in"

assign Ser_clk            to pins "Ser_clk"

assign GND1               to pins *

assign Reset              to pins *

assign OE_bar             to pins *
assign Ext_trig           to pins *

assign PWM                to pins *

assign Addr_0             to pins "Addr_0"
assign Addr_1             to pins *
assign Addr_2             to pins *
assign Addr_3             to pins *
assign Addr_4             to pins *

assign WE_bar             to pins *
assign Int                to pins *
assign Color              to pins *
assign Sat                to pins *

assign RyBy_bar           to pins "RyBy_bar"

assign VCC                to pins *
assign GND                to pins *

power  VCC, GND, GND1

family   CMOS_3V3

inputs   Ser_in, Ser_clk, Addr_0
outputs  Ser_out, RyBy_bar

nondigital Int, Color, Sat, OE_bar, Ext_trig, PWM
nondigital Addr_1, Addr_2, Addr_3, Addr_4, WE_bar, Reset

!********** assignments **********************

variable Data1(*) to groups Ser_in
variable Data2(*) to groups Ser_in
variable Data3(*) to groups Ser_in
variable Data4(*) to groups Ser_in
variable Data5(*) to groups Ser_in
variable Data6(*) to groups Ser_in
variable Data7(*) to groups Ser_in


!*******************   Disable information        *****************

disable Ser_out with Addr_0 to "0"

set load on groups  Ser_out to pull up
set ref on groups Addr_0, Ser_clk, Ser_in to dh 3.3, dl 0, rh 2, rl 1
set slew rate on groups Addr_0, Ser_clk, Ser_in to 25

!*******************************************************************
!*******************      VECTOR SECTION       *********************
!*******************************************************************


vector  Set_values        !set LED Analyser to known state
   set Addr_0             to "0"
   set Ser_clk            to "0"
   set Ser_in             to "0"
end vector

vector Keep_values        !Save input values.
   set Addr_0             to "k"
   set Ser_clk            to "k"
   set Ser_in             to "k"
end vector

vector Addr_0_high
   initialize to Keep_values
   set Addr_0          to "1"
end vector

vector Addr_0_low
   initialize to Keep_values
   set Addr_0          to "0"
end vector

vector Clock
   initialize to Keep_values
   set Ser_clk          to "t"
end vector

vector Clock_1
   initialize to Keep_values
   set Ser_clk          to "1"
end vector

vector Clock_0
   initialize to Keep_values
   set Ser_clk          to "0"
end vector

vector Start_bit
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_in_X
   initialize to Keep_values
   set Ser_in          to "x"
end vector

vector Ser_in_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector In_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector Ser_in_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector In_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Ser_out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector Out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector  Ser_out_x
   initialize               to Keep_values
   set   Ser_out            to "x"
end vector

vector  RyBy_bar_0
   initialize               to Keep_values
   set   RyBy_bar           to "0"
end vector

vector  RyBy_bar_1
   initialize               to Keep_values
   set   RyBy_bar           to "1"
end vector


!*******************************************************************
!*******************    SUBROUTINE SECTION     *********************
!*******************************************************************

sub Capture_Sequence_Data1             ! capture - send Byte to LA 0xC0
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data1(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data1(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Capture_Sequence_Data2     ! capture - send HiByte to LA Capture Time (msec)
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data2(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data2(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Capture_Sequence_Data3     ! capture - send LoByte to LA Capture Time (msec)
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data3(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data3(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Capture_Sequence_Data4      ! capture - send HiByte to LA  wait time (msec)
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data4(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data4(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Capture_Sequence_Data5     ! capture - send LoByte to LA  wait time (msec)
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data5(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data5(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Capture_Sequence_Data6    ! capture - send HiByte to LA  of No. Captures
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data6(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data6(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Capture_Sequence_Data7    ! capture - send LoByte to LA  of No.Captures
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_x drive Data7(1)      !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(2)      !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(3)      !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(4)      !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(5)      !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(6)      !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(7)      !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data7(8)      !Bit0
   execute Clock
   execute Clock
end sub

sub Pre_capture_acknowledge             !pre capture acknowledge
   execute Addr_0_high                  !Chip Select = 1
   execute Clock_0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1  receive 11010100  0xD5
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !0

end sub



sub Post_capture_acknowledge           !capture acknowledge
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_1                    !1      send 10111011  0xBB
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock

!wait 20u
!read back data from Ser_out

   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
   execute Clock
   execute Clock
   execute Ser_out_0                    !0
   execute Clock
   execute Clock
   execute Ser_out_1                    !1
end sub


sub Initialize_Capture    !Initialize Capture - send 10101010 0xAA to LA
   execute Addr_0_high
   execute Clock_0
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
! wait 1m
end sub

sub Reset_Comms                !Reset Communications- send 11111111 0xFF to LA
   execute Addr_0_low                  !Chip Select = 0
   execute Clock_0
   execute Ser_in_1                    !bit 7 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 6 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 5 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 4 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 3 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 2 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 1 = 1
   execute Clock
   execute Clock
   execute Ser_in_1                    !bit 0 = 1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Addr_0_high                 !Chip Select = 1
   wait 1m
end sub


!*******************************************************************
!*********************      UNIT SECTION       *********************
!*******************************************************************

!-------------------------------------------------------------------
!  Initialize all the inputs to a known value
!-------------------------------------------------------------------
unit "Initialize All"
    execute Set_values
end unit
!-------------------------------------------------------------------
!  Clear the input buffer of the Led Analyser to
!  ensure that the led analyser has detected no false
!  clock inputs
!-------------------------------------------------------------------
unit "Reset Communications"
        call Reset_Comms            !Send Reset Code x0FF
end unit
!-------------------------------------------------------------------
!   Unit to Initialize the Color Capture
!   Sends AA to the Led Analyser
!-------------------------------------------------------------------
unit "Initialize the LED Analyser"
        call Initialize_Capture   !Send Initialize Code x0AA
        homingloop 2000 times
          execute RyBy_bar_0 exit if pass
        end homingloop
        execute Addr_0_low                 !Addr_0=0
end unit

!-------------------------------------------------------------------
!   Unit to send the sequence capture data to the Analyser
!   7 bytes are sent to the Analyser in binary format:-
!   Byte 1 is the command 0xC0
!   Byte 2/3 is the Capture time in msec.  The range is 001 to 999 msec.
!   Byte 4/5 is the wait time in msec.  The range is 001 to 999 msec.
!   Byte 6/7 is the Number of Captures
!--------------------------------------------------------------------


unit "Capture Sequence"
      call Capture_Sequence_Data1         !Send 0xC0
      call Capture_Sequence_Data2         !Send 0xCapture Time
      call Capture_Sequence_Data3         !Send 0xCapture Time
      call Capture_Sequence_Data4         !Send 0xWait Time
      call Capture_Sequence_Data5         !Send 0xWait Time
      call Capture_Sequence_Data6         !Send 0xSequence Number High Byte
      call Capture_Sequence_Data7         !Send 0xSequence Number Low Byte

      homingloop 100000 times
         execute RyBy_bar_0 exit if pass     !wait until RYBY goes low
      end homingloop

      execute Addr_0_low                 !Addr_0=0
end unit

!
! End of test
!



